<?php

use Illuminate\Support\Arr;

if (!function_exists('arrayDot')) {
    function arrayDot($data, $key)
    {
        $result = [];
        $data = Arr::dot($data);

        foreach ($data as $index => $item) {
            $result[$key . $index] = $item;
        }

        return $result;
    }
}

if (!function_exists('admin')) {
    function admin()
    {
        return auth(\App\Providers\AdminServiceProvider::NAME);
    }
}

if (!function_exists('resolveView')) {
    function resolveView($folder = '', $view = '')
    {
        $pattern = '/(?:.*[\/|\\\\])?(.*)([A-Z].*)/';
        $folder = preg_replace($pattern, '$1', $folder);
        $name = preg_replace($pattern, '$2', $view);

        return strtolower($folder . ($name ? '.' . $name : ''));
    }
}

if (!function_exists('initCliEnv')) {
    function initCliEnv()
    {
        if (PHP_SAPI === 'cli' && config('database.connections.mysql.host') != '127.0.0.1') {
            config()->set('database.connections.mysql.host', '127.0.0.1');
        }
    }
}
